<template>
  <div>
    <!--面包屑-->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!--卡片-->
    <el-card class="box-card">
      <!--搜索-->
      <el-input v-model="getGoodsQuery.query" placeholder="请输入内容" clearable>
        <el-button slot="append" icon="el-icon-search" @click="findGoods"></el-button>
      </el-input>
      <el-button type="primary" @click="addGood">添加商品</el-button>
      <!--table表格-->
      <el-table :data="goodsList" border style="width: 100%; height: 100%" stripe>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="450px"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="100px"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="100px"></el-table-column>
        <el-table-column prop="add_time" label="创建时间">
          <template slot-scope="scope">
            {{scope.row.add_time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="props">
            <el-button size="mini" type="primary" class="el-icon-edit"></el-button>
            <el-button size="mini" type="danger" class="el-icon-delete"
                       @click="showDeleteGood(props.row.goods_id)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="getGoodsQuery.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="getGoodsQuery.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total" background>
      </el-pagination>
    </el-card>
    <!---->
    <!---->
    <!---->
    <!--删除商品弹框-->
    <el-dialog
      title="提示"
      :visible.sync="deleteDialogVisible"
      width="30%">
      <p>
        <i class="el-icon-warning"></i>
        此操作将永久删除该商品，是否继续
      </p>
      <span slot="footer" class="dialog-footer">
        <el-button @click="deleteDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="deleteGood">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data: function () {
    return {
      // 获取商品列表数据参数
      getGoodsQuery: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 获取到的商品列表
      goodsList: [],
      // 总计
      total: 0,
      // 监听弹出、隐藏删除提示框
      deleteDialogVisible: false,
      // 删除商品的id
      deleteId: ''
    }
  },
  created: function () {
    this.getGoods()
  },
  methods: {
    // 获取商品列表数据
    getGoods: async function () {
      var { data: res } = await this.$http.get('goods', { params: this.getGoodsQuery })
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品列表数据失败！')
      }
      this.goodsList = res.data.goods
      this.total = res.data.total
    },
    // 每页条数
    handleSizeChange(val) {
      this.getGoodsQuery.pagesize = val
      this.getGoods()
    },
    // 当前页
    handleCurrentChange(val) {
      this.getGoodsQuery.pagenum = val
      this.getGoods()
    },
    // 按照字符串搜索商品
    findGoods: function () {
      this.getGoods()
    },
    // 展示删除弹框
    showDeleteGood: function (id) {
      this.deleteDialogVisible = true
      this.deleteId = id
    },
    // 删除商品
    deleteGood: async function () {
      var { data: res } = await this.$http.delete('goods/' + this.deleteId)
      if (res.meta.status !== 200) {
        return this.$message.error('删除商品失败！')
      }
      this.$message.success('删除商品成功！')
      this.deleteDialogVisible = false
      this.getGoods()
    },
    // 添加商品信息
    addGood: function () {
      // this.showCard = false
      this.$router.push('/goods/add')
    }
  }
}
</script>
<style lang="less" scoped>
  .el-input{
    width: 300px;
    margin-right: 15px;
  }
  p{
    line-height: 30px;
  }
  i {
    color: #E6A23C;
    font-size: 30px;
  }
  .el-steps{
    margin-top: 15px;
  }
</style>
